package com.example.demo.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.R;
import com.example.demo.dto.PostDto;
import com.example.demo.entity.Balance;
import com.example.demo.entity.Post;
import com.example.demo.mapper.PostMapper;
import com.example.demo.service.PostAreaService;
import com.example.demo.service.PostService;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import javax.sql.DataSource;
import java.util.List;

@RestController
@RequestMapping("/post")
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostAreaService postAreaService;

    // @Bean
    // public MysqlxCrud.DataModel dataModel() throws Exception {
    //     return new MySQLBooleanPrefJDBCDataModel(dataSource, "ratings", "user_id", "item_id", "rating");
    // }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        postService.deletePost(id);
        return R.success("删除成功");
    }

    /**
     * 查到所有
     *
     * @return
     */
    @GetMapping("/all")
    public R<List<Post>> getAll() {
        List<Post> posts = postMapper.selectList(null);
        return R.success(posts);
    }

    /**
     * 分页查询帖子或根据名称查询帖子
     *
     * @param page
     * @param pageSize
     * @param postName
     * @return
     */
    @GetMapping("/page")
    public R<Page> Page(int page, int pageSize, String postName) {
        log.info("page = {},pageSize = {},postName = {}", page, pageSize, postName);

        Page<Post> allPost = postService.getAllPost(page, pageSize, postName);

        return R.success(allPost);
    }

    /**
     * 根据ID查询帖子
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<PostDto> getById(@PathVariable Long id) {
        log.info("根据id查询帖子信息...");

        PostDto byIdPost = postService.getByIdPost(id);

        return R.success(byIdPost);
    }


    /**
     * 增加帖子
     *
     * @param post
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody Post post) {
        log.info("新增帖子，帖子信息：{}", post.toString());

        postService.savePost(post);

        return R.success("新增帖子成功");
    }

    /**
     * 修改帖子
     *
     * @param post
     * @return
     */
    @PatchMapping
    @Transactional
    public R<String> add(@RequestBody Post post) {
        log.info("修改帖子，帖子信息：{}", post.toString());

        postService.updatePost(post);

        return R.success("修改帖子成功");
    }

    /**
     * AI智能推荐
     * @return
     */
    @RequestMapping("/ai")
    public R<List<Post>> ai(){
        List<Post> posts = postService.recommendAi();
        return R.success(posts);
    }

    /**
     * 对应地区的帖子
     * @param province
     * @return
     */
    @GetMapping("/province")
    public R<List<Post>> getBalanceArea(String province){

        List<Post> allBalanceArea = postAreaService.getAllPostArea(province);

        return R.success(allBalanceArea);
    }

}


